import * as $utils from '@/common/comFun/utils.js';

import layer from '@/common/comFun/layerFun.js';

export const setPageMinHeight = () => {
	return {
		'position': 'relative',
		'min-height': uni.getSystemInfoSync().windowHeight + 'px'
	}
}



export function request(parms, successCallback, errorCallback) {

	uni.getNetworkType({
		success: function(res) {
			if (res.networkType === 'none') {
				uni.showToast({
					title: '无网络连接',
					icon: 'none'
				});
				if (errorCallback) errorCallback({
					msg: '无网络连接'
				});
				return;
			}




			var param = JSON.stringify(parms);
			let requestCompleted = false; // 标记请求是否完成
			var url = 'http://101.37.31.198/8000/ask' ///Frame/SqlData?cmdname=' + cmd
			setTimeout(() => {
				if (!requestCompleted) {
					var obj = {
						type: 'NotCalled',
						title: '服务器繁忙,请重新询问'
					}
					if (successCallback) successCallback(obj);
				}
			}, 60000);
			// }, 60000);

			uni.request({
				url: url,
				data: param,
				timeout: 60000,
				method: 'POST',
				header: {
					'access-token': 'hzyc20250312Jhsc1s!#'
				},
				success: (ret, err) => {
					console.log(JSON.stringify(ret))
					if (ret) {
						// if (ret.data.status == 200) {
						// console.log(ret.data);
						// if (ret.data.status == 200) {
						requestCompleted = true
						if (ret.statusCode == 500) {
							if (errorCallback) errorCallback(ret);

						} else {

							if (successCallback) successCallback(ret.data);
						}
						// }

						// } else {
						// 	$utils.hideLoading();
						// 	let IPData = 'http://101.37.31.198/8000'
						// 	if (process.env.NODE_ENV === 'development') {
						// 		uni.showToast({
						// 			title: 'ask' + '\n' + IPData + '\n' + '错误代码1:' +
						// 				'\n' + JSON.stringify(ret),
						// 			icon: 'none'
						// 		});
						// 	} else {
						// 		layer.alert(JSON.stringify(ret.data.msg))
						// 	}

						// 	if (errorCallback) errorCallback(ret.data);
						// }
					}
				},
				fail: (err) => {
					requestCompleted = true;

				}

			})


		},
		fail: function() {
			uni.showToast({
				title: '无法获取网络状态',
				icon: 'none'
			});
			if (errorCallback) errorCallback({
				msg: '无法获取网络状态'
			});
			return;
		}
	})
}




const requestByUrlAPP = function() {
	return {
		LoginIP: function() {
			// return '39.105.222.87:10015'
			return 'mes.hzyc.org.cn:20050'
		},
		/**
		 * 获取协议类型 http:// 或 https://
		 */
		IPAgreement: function(data) {
			if (data) {
				return uni.setStorageSync('IPAgreement', data);
			} else {
				return uni.getStorageSync('IPAgreement');
				// return 'http://'
			}
		},
		UserInfo: function(data) {
			if (data) {
				return uni.setStorageSync('UserInfo', data);
			} else {
				return uni.getStorageSync('UserInfo');
			}
		},
		/**
		 * 根据是否传入值返回对应IP地址。
		 * '39.105.222.87:20000' 	正常请求接口
		 * '106.15.33.39:9981' 		PC商城资源需要
		 * @param {string} data 有值就行
		 */
		ShopServiceIP: function(data) {
			if (data) {
				return '39.105.222.87:20000';
			} else {
				return '106.15.33.39:9981';
			}
		},
		getUserInfo() {
			if (uni.getStorageSync('UserInfo')) {
				return JSON.parse(uni.getStorageSync('UserInfo'))
			} else {
				return null;
			}
		},
		serverIP: function(ip) {
			if (ip) {
				return uni.setStorageSync('serverIP', ip);
			} else {
				return uni.getStorageSync('serverIP');
			}
		},
		/** 请求方式1：回调直接获取接口数据
		 * @param cmd 	接口名
		 * @param param 	参数
		 * @param successCallback 	成功的回调
		 * @param errorCallback 	失败的回调
		 * @param IP 	IP地址（传入则使用传入的IP地址，不传则使用存储的IP地址）
		 * @param data 	区分走哪个  1 -- '?cmdname='; 2 --'?cmd=' ; 不传 -- 无 
		 */
		requestByUrl: function(requestUrl, param, successCallback, errorCallback, IP, data) {
			uni.onNetworkStatusChange(function(res) {
				if (!res.isConnected) {
					uni.showToast({
						title: '网络连接不可用！',
						icon: 'none'
					});
				}
				return false
			});
			var url = ''
			if (IP) {
				if (data == 1) {
					url = IP + '?cmdname=' + cmd;
				} else if (data == 2) {
					url = IP + '?cmd=' + cmd;
				} else {
					url = IP
				}
			} else {
				url = requestByUrlAPP.IPAgreement() + requestByUrlAPP.serverIP() + requestUrl;
			}
			if (requestByUrlAPP.getUserInfo()) {
				if (!param.hasOwnProperty("P_UserID")) {
					param.P_UserID = requestByUrlAPP.getUserInfo().UserID;
				}
				if (!param.hasOwnProperty("P_UserName")) {
					param.P_UserName = requestByUrlAPP.getUserInfo().UserName;
				}
				if (!param.hasOwnProperty("P_UserCode")) {
					param.P_UserCode = requestByUrlAPP.getUserInfo().UserCode;
				}
				if (!param.hasOwnProperty("P_EmplID")) {
					param.P_EmplID = requestByUrlAPP.getUserInfo().EmplID;
				}
			}
			uni.request({
				url: url,
				data: param,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
				},
				success: (ret) => {

					if (ret) {
						if (ret.data.status == 200) {
							if (successCallback) successCallback(ret.data.data);
						} else {
							$utils.hideLoading();

							// if (process.env.NODE_ENV === 'development') {

							// 	uni.showToast({
							// 		title: requestByUrlAPP.serverIP() + '\n' + '错误代码1:' +
							// 			'\n' + JSON
							// 			.stringify(ret),
							// 		icon: 'none'
							// 	});
							// }

							if (errorCallback) errorCallback(ret.data);
						}
					} else {
						$utils.hideLoading();
						if (errorCallback) {
							errorCallback(err);
						} else {
							console.log(JSON.stringify(err))

						}
					}
				}
			});
		}
	}
}()
export default requestByUrlAPP